package com.qnw.CardGroupManagement;

import android.content.ClipboardManager;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import com.ygo.feihua.Gongju;
import com.ygo.feihua.R;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import android.support.v4.util.ArrayMap;
import android.os.*;
import android.graphics.*;
import com.qnw.CardGroupManagement.util.*;
import com.ygo.feihua.util.*;
import android.util.*;
import com.ygo.feihua.Management.*;
public class A5 extends AppCompatActivity implements OnClickListener
{
	
	//卡图
	private ImageView im_kt;
	//卡密
	private TextView tx_km;
	//归属
	private TextView tx_gs;
	//卡片类别
	private TextView tx_lb;
	//等级
	private TextView tx_xj;
	//种族
	private TextView tx_zz;	
	//属性
	private TextView tx_sx;
	//攻击力
	private TextView tx_gjl;
	//防御力
	private TextView tx_fyl;
	private ViewPager a5_pag;
	private TabLayout a5_tab;
	
	private Card card;
	
	private String cardId;

	private Gongju gj;
	private List<Map<String,Object>> fg=new ArrayList<Map<String,Object>>();
	private Find_tab_Adapter fAdapter;

	private Toolbar tool;

	public Card getCard() {
		return card;
	}

	@Override
	public void onClick(View p1) {
		switch(p1.getId()){
			case R.id.a5_tx_km:
				gj.fzMessage(cardId);
				SnackbarUtil.ShortSnackbar(p1,"已复制到剪贴板",SnackbarUtil.white,0xFF008cf9).show();
				break;
			case R.id.toolbar:
				gj.fzMessage(tool.getTitle().toString());
				SnackbarUtil.ShortSnackbar(p1,"已复制到剪贴板",SnackbarUtil.white,0xFF008cf9).show();
				break;
		}
		// TODO: Implement this method
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		Log.e("未加载布局", "未加载");
		setContentView(R.layout.a5_layout);	
		Log.e("进入A5", "进入A5");

		initView();
		initCard();

		Log.e("效果初始化完毕", "效果初始化完毕");
	}

	private void initView() {
		SdkInitManagement.getDx().initBmobSdk();
		cardId = getIntent().getStringExtra("km");

		gj=Gongju.getdx(this);

		im_kt = (ImageView)findViewById(R.id.a5_im_kt);
		tx_km = (TextView)findViewById(R.id.a5_tx_km);
		tx_gs = (TextView)findViewById(R.id.a5_tx_gs);
		tx_lb = (TextView)findViewById(R.id.a5_tx_lb);
		tx_xj = (TextView)findViewById(R.id.a5_tx_xj);
		tx_zz = (TextView)findViewById(R.id.a5_tx_zz);
		tx_sx = (TextView)findViewById(R.id.a5_tx_sx);
		tx_gjl = (TextView)findViewById(R.id.a5_tx_gjl);
		tx_fyl = (TextView)findViewById(R.id.a5_tx_fyl);

		a5_tab=(TabLayout) findViewById(R.id.a5_tab);
		a5_pag=(ViewPager) findViewById(R.id.a5_pag);
		tool=(Toolbar) findViewById(R.id.toolbar);
		
		gj.cshToolbar(tool,"返回");
		
		tx_km.setOnClickListener(this);
		tool.setOnClickListener(this);
		
		initPag();
		// TODO: Implement this method
	}

	public void initCard()
	{
		SQLiteDatabase sjk=SQLiteDatabase.openOrCreateDatabase(SharedPreferenceUtil.getCdbpath(), null);//数据库不存在则创建，存在则取得对象的实例

		Cursor datas=sjk.query("datas", new String[]{"id","ot","type","level","attribute","race","atk","def"}, "id = ?", new String[]{cardId}, null, null, null);
		Cursor texts=sjk.query("texts", new String[]{"id","name","desc"}, "id = ?", new String[]{cardId}, null, null, null);
		if (datas.getCount() != 0 && texts.getCount() != 0)
		{//获取集合的个数，如果不为0(没查到则为0)
			datas.moveToNext();//移动到第一行，默认是0，没有写这句，直接获取值会抛异常
			texts.moveToNext();
			
			card=new Card();
			card.setId(datas.getString(0));
			card.setName(texts.getString(1));
			card.setDesc(texts.getString(2));
			card.setOt_16(datas.getString(1));
			card.setCard_type_16(datas.getInt(2));
			card.setLevel_16(datas.getInt(2),datas.getString(3));
			card.setAttribute_16(Integer.toHexString(datas.getInt(4)));
			card.setRace_16(datas.getString(5));
			card.setAtk_16(datas.getInt(2),datas.getString(6));
			card.setDef_16(datas.getInt(2),datas.getString(7));
			
			datas.close();
			texts.close();
		}else{
			datas.close();
			texts.close();
			gj.show("未查询到该卡信息");
			finish();
		}
		
		Gongju.setKt(this, im_kt, cardId);
		tool.setTitle(card.getName());
		tx_gs.setText(card.getOt());
		String s="";
		for(String ss:card.getCard_type()){
			s+=ss+"|";
		}
		s=s.substring(0,s.length()-1);
		tx_lb.setText(s);
		tx_sx.setText(card.getAttribute());
		tx_xj.setText(card.getLevel());
		tx_zz.setText(card.getRace());
		tx_gjl.setText(card.getAtk());
		tx_fyl.setText(card.getDef());
		tx_km.setText(cardId);
		
	}

	private void initPag(){
	
		FragmentXiaoguo f1=new FragmentXiaoguo();
		Map<String,Object> map1=new ArrayMap<String,Object>();
		map1.put("fr",f1);
		map1.put("title","效果");
		fg.add(map1);	
		
		FragmentCombo f3=new FragmentCombo();
		Map<String,Object> map3=new ArrayMap<String,Object>();
		map3.put("fr",f3);
		map3.put("title","combo");
		fg.add(map3);
		
		FragmentZhaohuanci f4=new FragmentZhaohuanci();
		Map<String,Object> map4=new ArrayMap<String,Object>();
		map4.put("fr",f4);
		map4.put("title","召唤词");
		fg.add(map4);
		
		FragmentZiliao f5=new FragmentZiliao();
		Map<String,Object> map5=new ArrayMap<String,Object>();
		map5.put("fr",f5);
		map5.put("title","资料");
		fg.add(map5);
		
        //设置TabLayout的模式
        a5_tab.setTabMode(TabLayout.MODE_FIXED);
        fAdapter = new Find_tab_Adapter( getSupportFragmentManager());

        //viewpager加载adapter
        a5_pag.setAdapter(fAdapter);
		//缓存两个页面
		a5_pag.setOffscreenPageLimit(5);
        //TabLayout加载viewpager
       a5_tab.setupWithViewPager(a5_pag);
	}

	 class Find_tab_Adapter extends FragmentPagerAdapter {
		public Find_tab_Adapter(FragmentManager fm) {
			super(fm);
		}
		

		@Override
		public Fragment getItem(int position) {
			return (Fragment)fg.get(position).get("fr");
		}

		@Override
		public int getCount() {
			return fg.size();
		}

		//此方法用来显示tab上的名字
		@Override
		public CharSequence getPageTitle(int position) {

			return (String)fg.get(position).get("title");
		}
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.a5, menu);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
			case R.id.m_add:
				Intent intent =new Intent(this,ComboAdd.class);
				intent.putExtra("km",cardId);
				startActivity(intent);
				break;
			case R.id.m_kpxx:
				copyCardMessage();
				break;
				case R.id.m_bjkpxx:
				{
					Intent i=new Intent(A5.this, G5.class);
					i.putExtra("km", cardId);
					startActivity(i);
					break;
				}
		}
		return true;
	}
	
	private void copyCardMessage(){
		String ii = "卡名:" + card.getName()
			+"\n" + "卡密:" + cardId 
			+"\n" + "归属:" + tx_gs.getText() 
			+"\n" + "类别:" + tx_lb.getText() 
			+"\n" + "星级:" + tx_xj.getText() 
			+"\n" + "种族:" + tx_zz.getText() 
			+"\n" + "属性:" + tx_sx.getText() 
			+"\n" + "攻击力:" + tx_gjl.getText() 
			+"\n" + "防御力:" + tx_fyl.getText();

		gj.fzMessage(ii + "\n卡片描述:\n" + card.getDesc());

		gj.show("已复制到剪切板");
	}
	
}
